package com.android.baselibrary.utils

import android.graphics.Rect
import android.view.View
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.RecyclerView
import androidx.recyclerview.widget.StaggeredGridLayoutManager

class GridSpaceItemDecoration(private val spanCount:Int, private val spacing:Int):RecyclerView.ItemDecoration() {
    override fun getItemOffsets(outRect: Rect, view: View, parent: RecyclerView, state: RecyclerView.State) {
        val layoutParams: GridLayoutManager.LayoutParams =
            view.layoutParams as GridLayoutManager.LayoutParams
        val spanIndex: Int = layoutParams.spanIndex
        val position: Int = parent.getChildAdapterPosition(view)
        outRect.top = spacing
       /* if (position == 0 || position == 1) {
            outRect.top = spacing * spanCount
        } else {
            outRect.top = 0
        }*/
        if (spanIndex % spanCount == 0) { //左边
            outRect.left = spacing
            outRect.right = spacing / 2
        } else if (spanIndex % spanCount == 1){// 中间
            outRect.left = spacing / 2
            outRect.right = spacing/2
        }else{ //左边
            outRect.left = spacing / 2
            outRect.right = spacing
        }

    }
}